import React from "react";
import {Select,Button, Modal, Form,Checkbox, Col, Row, Input} from 'antd/lib';
import Collapse from 'antd/lib/collapse';
import notification from "@/services/notification";
import {get} from "lodash";
import Uploads from "@/services/uploads";
import 'antd/dist/antd.less'
import moment from "moment";
import Customer from "@/services/customer";
import DatePicker from "antd/lib/date-picker";
import locale from 'antd/es/date-picker/locale/zh_CN';
import { currentUser} from "@/services/auth";
import ContractSeal from "@/services/contractseal";
import Contracts from "@/services/contracts";
import ContractApply from "@/services/contractapply";
import WarningOutlined from "@ant-design/icons/WarningOutlined";
import Attaches from "@/services/attaches";
import {StarOutlined, UploadOutlined} from '@ant-design/icons';
import GlobalFunctions from '@/store/globalFunctions';
import Upload from "antd/lib/upload";
import { inject, observer } from 'mobx-react';
const { confirm } = Modal;
const { Panel } = Collapse;

class SealTable extends React.Component {
  state = {
    loading: true,
    contractsdata:[],
    departmentdata:[],
    employeedata:[],//HRP职员表
    handledeptdata:[],
  };
  // componentDidMount() {
  //   const userId = this.props.userId || currentUser.id;
  // }
  componentWillMount() {
    let id=  this.props.id.replace('daz','');
    let param={
      billtype: "contractseal",
      id: id,
    }
    Contracts.getsellist(param).then(data => {
      this.setState({
        contractsdata: data,
        loading: false,
      });
    });
      //HRP部门表
    param={
        code: "",
        name: "",
        userid: currentUser.id,
      }
      Customer.querydepartment(param).then(data => {
        this.setState({
          departmentdata: data,
        });
      });
    //HRP职员表
    param={
      code: "",
      name: "",
      userid: currentUser.id,
    }
     Customer.queryshenjiemployee(param).then(data => {
      this.setState({
    //    employeeid:eid,
        employeedata: data,
      });
    });
    param={code: "",
    name: "",
    orgunit_id:this.props.contractSealStore.contractSealModel.departmentid,
    userid: this.props.contractSealStore.contractSealModel.handledept,
  }
    this.refreshData(param);
  }
  refreshData(param) {
     Customer.queryemployee(param).then(data => {
        this.setState({
          handledeptdata: data,
        });
    });

  }


 bill_dateonchange = (value) => {
  if (GlobalFunctions.isEmptyobj(value)) {
    return;
  }
  this.props.contractSealStore.contractSealModel.setBill_date(value);
  if (!this.props.contractSealStore.contractSealModel.isnew) {
    return;
  }
  this.props.contractSealStore.getcode("contractseal",value,0,"code");
 };


  departmentidchange= (value) => {    
    this.props.contractSealStore.contractSealModel.setDepartmentid(value);

    let param={code: "",
    name: "",
    orgunit_id:value,
    userid: currentUser.id,
  }
    this.refreshData(param);
 };
  contractchange= (value) => {
    let item = this.state.contractsdata.filter((p) => {
        return p.id === value;
      });
      if(item.length<1){
        return;
      }
      this.props.contractSealStore.contractSealModel.setContractsid(item[0]);
      let param={code: "",
        name: "",
        orgunit_id:item[0].departmentid,
        userid: currentUser.id,
      }
      this.refreshData(param);
   };
  fieldedit(field){
    if(field==="archive" && this.props.sealtype==="1" && !this.props.contractSealStore.contractSealModel.archive){
      return false;
    }
    if(!this.props.contractSealStore.contractSealModel.isedit){
      return true;
    }
    if(this.props.contractSealStore.contractSealModel.colcontrol){
      if(this.props.contractSealStore.contractSealModel.allow_col.indexOf(field)>=0){
        return false;
      }else{
        return true;
      }
    }else{
      return false;
    }
  }
  render() {
    if (this.state.loading) return null;
    return(
      <div>
        <Collapse defaultActiveKey={['1', '2', '3']} >
          <Panel header="基本情况" key="1">

      <Row style={{ marginBottom: 5 }}>
              <Col span={2}><div style={{ marginTop: "5px", textAlign: "right", marginRight: "10px" }}>
                合同编码:</div></Col>
              <Col span={6}>
              <Select 
                showSearch
                optionFilterProp="children"
              style={{ width:  "100%" }} 
              value={this.props.contractSealStore.contractSealModel.contractsid} 
              onChange={this.contractchange}  
              disabled ={this.fieldedit("contractsid")} >
              {(this.state.contractsdata || []).map(item => (
                <Select.Option key={item.id} value={item.id}>{item.code}</Select.Option>
              ))}
              </Select>
          </Col>
          <Col span={2}><div style={{ marginTop: "5px", textAlign: "right", marginRight: "10px" }}>
          名称:</div></Col>
              <Col span={6}>
            <Input
                value={this.props.contractSealStore.contractSealModel.name}
                style={{ width:  "100%" }}
                onChange={e => this.props.contractSealStore.contractSealModel.setName(e.target.value)}
                disabled ={true}
                placeholder="名称"
              />
				  </Col>
        </Row>
        <Row style={{ marginBottom: 5 }}>
              <Col span={2}><div style={{ marginTop: "5px", textAlign: "right", marginRight: "10px" }}>
              院长办公室:</div></Col>
              <Col span={6}>
              <Select style={{ width:  "100%" }}
                      showSearch
                      optionFilterProp="children"
                      value={this.props.contractSealStore.contractSealModel.employeeid}
                      onChange={value => this.props.contractSealStore.contractSealModel.setEmployeeid(value)} 
                      disabled ={this.fieldedit("employeeid")}>
              {(this.state.employeedata || []).map(item => (
                <Select.Option key={item.id} value={item.id}>{item.name}</Select.Option>
              ))}
              </Select>
          </Col>
          <Col span={2}><div style={{ marginTop: "5px", textAlign: "right", marginRight: "10px" }}>
          经办部门:</div></Col>
              <Col span={6}>
              <Select style={{ width:  "100%" }}
                      showSearch
                      optionFilterProp="children"
                      value={this.props.contractSealStore.contractSealModel.departmentid}
                      onChange={this.departmentidchange} 
                      disabled ={this.fieldedit("departmentid")}>
              {(this.state.departmentdata || []).map(item => (
                <Select.Option key={item.id} value={item.id}>{item.name}</Select.Option>
              ))}
              </Select>
          </Col>
        </Row>
        <Row style={{ marginBottom: 5 }}>
          <Col span={2}><div style={{ marginTop: "5px", textAlign: "right", marginRight: "10px" }}>
          用印经办人:</div></Col>
              <Col span={6}>
              <Select style={{ width:  "100%" }}
                      showSearch
                      optionFilterProp="children"
                      value={this.props.contractSealStore.contractSealModel.handledept}
                      onChange={value => this.props.contractSealStore.contractSealModel.setHandledept(value)} 
                      disabled ={this.fieldedit("handledept")}>
              {(this.state.handledeptdata || []).map(item => (
                <Select.Option key={item.id} value={item.id}>{item.name}</Select.Option>
              ))}
              </Select>
          </Col>
        </Row>
        <Row style={{ marginBottom: 5 }}>
              <Col span={2}><div style={{ marginTop: "5px", textAlign: "right", marginRight: "10px" }}>
              盖章几份:</div></Col>
              <Col span={6}>
              <Input
                value={this.props.contractSealStore.contractSealModel.sealcount}
                style={{ width:  "100%" }}
                onChange={e => this.props.contractSealStore.contractSealModel.setSealcount(e.target.value)}
                onkeyup="this.value=this.value.replace(/\D/g,'')"
                onafterpaste="this.value=this.value.replace(/\D/g,'')"
                type="number"
               disabled ={this.fieldedit("sealcount")}
                placeholder="盖章几份"
              />
				  </Col>
          <Col span={2}><div style={{ marginTop: "5px", textAlign: "right", marginRight: "10px" }}>
          盖章时间:</div></Col>
              <Col span={6}>
              <DatePicker
              locale={locale}
              value={this.props.contractSealStore.contractSealModel.sealdate}
              style={{ width:  "100%" }}
               onChange={value =>  this.props.contractSealStore.contractSealModel.setSealdate(value)}
              placeholder="盖章时间"
              disabled ={this.fieldedit("sealdate")}
              autoFocus
            />
				  </Col>
        </Row>
        <Row style={{ marginBottom: 5 }}>
              <Col span={2}><div style={{ marginTop: "5px", textAlign: "right", marginRight: "10px" }}>
              记录事项:</div></Col>
              <Col span={6}>
              <textarea
                rows="5" cols="60"
                textAlignVertical="top"
                height="100px"
                value={this.props.contractSealStore.contractSealModel.message}
                onChange={e => this.props.contractSealStore.contractSealModel.setMessage(e.target.value)}
                placeholder="记录事项"
                disabled ={this.fieldedit("message")}
              />
				  </Col>
        </Row>
        </Panel>
        <Panel header="归档信息" key="2">
        <Row style={{ marginBottom: 5 }}>
              <Col span={2}><div style={{ marginTop: "5px", textAlign: "right", marginRight: "10px" }}>
              是否归档:</div></Col>
              <Col span={6}>
          <Checkbox
            checked ={this.props.contractSealStore.contractSealModel.archive}
            style={{ width:  "100%" }}
            onChange={value =>this.props.contractSealStore.contractSealModel.setArchive(value.target.checked)}
            disabled ={this.fieldedit("archive")}
            autoFocus
          />
				  </Col>
          </Row>
        </Panel>
        </Collapse>
      </div>
    );
  };
}

export default inject('contractSealStore')(observer(SealTable));